package CutLoad.web.menu
{
	import CutLoad.Base;
	import CutLoad.CutLoad;
	import CutLoad.components.container.ContainerH;
	import CutLoad.components.container.ContainerV;
	import CutLoad.cookies.CArray;
	import CutLoad.cookies.Painter;
	
	import flash.events.MouseEvent;

	/**
	 * 二组分菜单，样式1
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CTwoClassification_1 extends Base
	{
		private var arrOneData:Array;
		private var arrTwoData:Array;
		//获得一级菜单分类样式，调用的方法
		private var funGetOneClassificationItem:Function;
		//获得二级菜单分类样式，调用的方法
		private var funGetTwoClassificationItem:Function;
		//背景
		private var bak:Base;
		//
		private var container1:ContainerH;
		//
		private var twoSpace:Number;
		//保存二级分类
		private var arrTwoContainer:CArray;
		//保存生成以后的最高值,因为一级分类在下边，注册点却在左上角
		private var maxH:Number=0;
		//点击回调方法
		private var FunClickItem:Function,FunOverItem:Function,FunOutItem:Function;
		private var oneSpace:Number;
		/**
		 * 初始化 
		 * @param oneSpace
		 * @param twoSpace
		 * @param oneData
		 * @param twoData
		 * @param funGetOneClassificationItem
		 * @param funGetTwoClassificationItem
		 * @param funGetBak
		 * 
		 * 这么多参数，还是看实例演示：
		 * var menu:CTwoClassification_1=new CTwoClassification_1(
				//一级分类间距
				5,
				//二级分类单距
				5,
				//一级菜单项
				["一级分类1","一级分类2","一级分类3"],
				//二级菜单项
				[
					[
						["1子分类1","a1"],
						["1子分类2","a2"],
						["1子分类3","a3"],
						["1子分类4","a4"],
						["1子分类5","a5"],
						["1子分类6","a6"],
						["1子分类7","a7"]
					],
					[
						["2子分类1","b1"],
						["2子分类2","b2"],
						["2子分类3","b3"],
					],
					[
						["3子分类1","c1"],
						["3子分类2","c2"],
					]
					
				],
				//生成一级菜单的样式
				function(label:String,data:*):CButton{
					var btn:CButton=new CButton();
					btn.init(label,false,true,true,true);
					return btn;
				},
				//生成二级菜单的样式
				function(label:String,data:*):CButton{
					var btn:CButton=new CButton();
					btn.init(label,false,true,true,true);
					return btn;
				},
				//生成背景的样式
				function():Base{
					return Base.getBase(Painter.getRandLineRect(500,500,4,20,RandNO.getRndColor32()));
				}
			);
			this.addChild(menu)
		 * 
		 * 
		 */		
		public function CTwoClassification_1(oneSpace:Number,twoSpace:Number,oneData:Array,twoData:Array,funGetOneClassificationItem:Function,funGetTwoClassificationItem:Function,funGetBak:Function=null)
		{
			if(oneData.length!=twoData.length) throw new Error("(oneData.length!=twoData.length   !!!");
			this.twoSpace=twoSpace;
			this.arrOneData=oneData;
			this.arrTwoData=twoData;
			this.funGetOneClassificationItem=funGetOneClassificationItem;
			this.funGetTwoClassificationItem=funGetTwoClassificationItem;
			//
			this.oneSpace=oneSpace;
			//
			container1=new ContainerH();
			container1.init({w:0,isAuto:false,xspace:oneSpace,yspace:0});
			//
			if(funGetBak==null){
				funGetBak=function():Base{
					return Painter.drawRect(10,10,0x000000,0);
				}
			}
			var obj:Object=funGetBak();
			if(!(obj is Base)) throw new Error("参数：funGetBak  返回类型不是Base!!");
			bak=obj as Base;
			bak.show(this);
			bak.setWH(1,1);
			//
			createItem();
			update();
		}
		/**
		 * 设置点击菜单项的回调方法 
		 * @param fun  格式:function(type:Number,lable:String,data:String,me:CTwoClassification_1)   注:type=1时为一级菜单,type==2时为二级菜单
		 * 
		 */		
		public function setFunClickItem(fun:Function):void{
			FunClickItem=fun;	
		}
		/**
		 * 设置鼠标放上菜单项的回调方法 
		 * @param fun 格式:function(type:Number,lable:String,data:String,me:CTwoClassification_1)   注:type=1时为一级菜单,type==2时为二级菜单
		 * 
		 */		
		public function setFunOverItem(fun:Function):void{
			FunOverItem=fun;
		}
		/**
		 * 设置鼠标离开菜单项的回调方法 
		 * @param fun 格式:function(type:Number,lable:String,data:String,me:CTwoClassification_1)   注:type=1时为一级菜单,type==2时为二级菜单
		 * 
		 */		
		public function setFunOutItem(fun:Function):void{
			FunOutItem=fun;
		}
		/**
		 * 返回一级菜单的水平排列容器 
		 * @return 
		 * 
		 */		
		public function getOneContainer():ContainerH{
			return container1;
		}
		/**
		 * 设置背景大小，如果不调用这个方法，
		 * @param w
		 * @param h
		 * 
		 */		
		public function setBakWH(w:Number,h:Number):void{
			bak.setWH(w,h);
		}
		/**
		 * 返回二级菜的垂直排列容器 
		 * @return 返回的是一个ContainerV对象数组
		 * 
		 */		
		public function getTwoContainer():CArray{
			return arrTwoContainer;
		}
		/**
		 * 生成需要所有可视对象 
		 * 
		 */		
		protected function createItem():void{
			arrTwoContainer=new CArray();
			//生成大分类
			for(var i:Number=0;i<arrOneData.length;i++){
				var item:Base=funGetOneClassificationItem(arrOneData[i],i);
				item.setOtherInfo({CTwoClassification_1_tye:1,CTwoClassification_1_label:arrOneData[i],CTwoClassification_1_data:i+""});
				item.addEventListener(MouseEvent.CLICK,click_item);
				item.addEventListener(MouseEvent.MOUSE_OVER,over_item);
				item.addEventListener(MouseEvent.MOUSE_OUT,out_item);
				container1.append(item,i!=arrOneData.length-1?false:true);
			}
			//
			container1.setTakeWH();
			//container1.showGuide()
			var arrTem:CArray=container1.getChildeGroup();
			arrTem.forEach(function(item:Object,index:Number):void{
				var arrTem123:Array=arrTwoData[index]
				//生成二级分类
				var container2Item:ContainerV=new ContainerV();
				container2Item.init({w:0,h:0,isAuto:false,xspace:0,yspace:twoSpace});
				for(var i:Number=0;i<arrTem123.length;i++){
					var baseTemItem:Base=funGetTwoClassificationItem(arrTem123[i][0],arrTem123[i][1])
					baseTemItem.addEventListener(MouseEvent.CLICK,click_item);
					baseTemItem.setOtherInfo({CTwoClassification_1_tye:2,CTwoClassification_1_label:arrTem123[i][0],CTwoClassification_1_data:arrTem123[i][1]+""});
					container2Item.append(baseTemItem,i!=arrTem123.length-1?false:true);
				}
				container2Item.setTakeWH();
				//container2Item.showGuide()
				if(maxH<container2Item.getShowHeight()){
					maxH=container2Item.getShowHeight()
				}
				arrTwoContainer.add(container2Item);
			})
			maxH+=container1.getShowHeight();
			//
		   //画出中心
		   //this.addChild(Painter.drawCircle(20,0xff0000,1));
		}
		/**
		 * 获得除掉背景的高度 
		 * @return 
		 * 
		 */		
		public function getMenuHeight():Number{
			return maxH;
		}
		/**
		 * 放好位置 
		 * 
		 */		
		protected function update():void{
			container1.setXY(0,maxH,this);
			var arrTem:CArray=container1.getChildeGroup();
			var me:CTwoClassification_1=this;
			arrTem.forEach(function(item:Object,index:Number):void{
				var baseTem:Base=item as Base;
				//放置到正确位置
				var cTem:ContainerV=arrTwoContainer.getOne(index) as ContainerV;
				//CutLoad.CutLoad.debug((baseTem.y)+"");
				cTem.setXY(baseTem.x+container1.x,baseTem.y+container1.y-cTem.getShowHeight(),me);
			});
			this.setBakWH(this.getShowWidth(),this.getShowHeight());
			//
			//
		}
		private function click_item(e:MouseEvent):void{
			var obj:Object=(e.target as Base).getOtherInfo();
			var type:Number=obj.CTwoClassification_1_tye;
			var label:String=obj.CTwoClassification_1_label;
			var data:String=obj.CTwoClassification_1_data;
			if(FunClickItem!=null){
				try{
					FunClickItem(type,label,data,this);
				}catch(e:ArgumentError){
					trace("setFunClickItem  里的回调函数，需要4个参数：格式:function(type:Number,lable:String,data:String,me:CTwoClassification_1)   注:type=1时为一级菜单,type==2时为二级菜单");
				}
			}
		}
		private function over_item(e:MouseEvent):void{
			var obj:Object=(e.target as Base).getOtherInfo();
			var type:Number=obj.CTwoClassification_1_tye;
			var label:String=obj.CTwoClassification_1_label;
			var data:String=obj.CTwoClassification_1_data;
			if(FunOverItem!=null){
				try{
					FunOverItem(type,label,data,this);
				}catch(e:ArgumentError){
					trace("setFunClickItem  里的回调函数，需要4个参数：格式:function(type:Number,lable:String,data:String,me:CTwoClassification_1)   注:type=1时为一级菜单,type==2时为二级菜单");
				}
			}
		}
		private function out_item(e:MouseEvent):void{
			var obj:Object=(e.target as Base).getOtherInfo();
			var type:Number=obj.CTwoClassification_1_tye;
			var label:String=obj.CTwoClassification_1_label;
			var data:String=obj.CTwoClassification_1_data;
			if(FunOutItem!=null){
				try{
					FunOutItem(type,label,data,this);
				}catch(e:ArgumentError){
					trace("setFunClickItem  里的回调函数，需要4个参数：格式:function(type:Number,lable:String,data:String,me:CTwoClassification_1)   注:type=1时为一级菜单,type==2时为二级菜单");
				}
			}
		}
	}
}